package com.copy;

import org.springframework.beans.BeanUtils;

/**
 * 属性复制工具类
 */
public class CopyUtil {

    public static <T,R> R copy(T source, Class<R> classType){
        if (source == null){
            return null;
        }
        R target = null;
        try {
            target = classType.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        BeanUtils.copyProperties(source, target);
        return target;
    }

    public static <T,R> R copy(T source, R target){
        if (source == null){
            return null;
        }
        BeanUtils.copyProperties(source, target);
        return target;
    }

}
